﻿using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Dev.Common;
using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class ByjlController : BaseController
    {   //
        // GET: /Dev/Byjl/

        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Byjl_BillNo", //主键
                urls = DevHelper.GetIndexUrls("Byjl"),
                resx = DevHelper.GetIndexResx("保养记录"),
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems()
                },
                form = new
                {
                    Byjl_BillNo = "",
                    Byjl_Sbbh = ""
                }

            };
            return View(model);
        }
         [MvcMenuFilter(false)]
        public ActionResult Edit(string id)
        {
            var data = new ByjlApiController().GetEditMaster(id);
            var codeService = new sys_codeService();

            var result = new List<dynamic>();
            result.Add(new { value = 1, text = "是" });
            result.Add(new { value = 0, text = "否" });

            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                urls = DevHelper.GetEditUrls("Byjl"),
                resx = DevHelper.GetEditResx("保养记录"),
                dataSource = new
                {
                    // iswx = result
                },
                defaultForm = new b_Djjl().Extend(new
                {
                    Byjl_BillNo = id,
                    Byjl_Ywrq = DateTime.Now.ToShortDateString(),
                    Byjl_Byfy = 0,
                    Byjl_Bygs = 0,
                    Byjl_Byr = "",
                    Byjl_Byyq = "",
                    Byjl_Sbbh = "",
                    Byjl_Bmbh = ""
                }),
                defaultRow = new
                {
                },
                setting = new
                {
                    postFormKeys = new string[] { "Byjl_BillNo" },
                    postListFields = new string[] { "Byjlmx_BillNo", "Byjlmx_Flbh", "Byjlmx_Bybw", "Byjlmx_Byxm", "Byjlmx_Bynr", "Byjlmx_Fzry", "Byjlmx_Jcry", "Byjlmx_Remark" }
                }
            };
            return View(model);
        }
    }


    public class ByjlApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Byjl_BillNo'>
                <select>
                   a.*,b.Sbda_Sbmc
                </select>
                <from>
                   b_Byjl a Left Join b_Sbda b on b.Sbda_Sbbm = a.Byjl_Sbbh
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Byjl_BillNo'   cp='like'  ></field>
                    <field name='Byjl_Bmmc'  cp='like'  ></field>
                    <field name='Byjl_Byr'  cp='like'  ></field>
                    <field name='Byjl_Ywrq' cp='daterange'></field>
                    <field name='A.ApprovePerson ' cp='daterange'></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new b_ByjlService().GetDynamicListWithPaging(pQuery);
            return list;

        }

        public string GetNewBillNo()
        {
            return new b_ByjlService().GetNewKey("Byjl_BillNo", "dateplus");
        }
        public string GetNewRowId(int id, string BillNo)
        {
            return new b_ByjlmxService().GetNewKey("Byjlmx_Flbh", "maxplus", id, ParamQuery.Instance().AndWhere("Byjlmx_BillNo", BillNo));
        }

        public dynamic GetEditMaster(string id)
        {
            b_Byjl obj = new b_ByjlService().GetModel(ParamQuery.Instance().AndWhere("Byjl_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new b_ByjlService().ScrollKeys("Byjl_BillNo", id.ToString())
            };
        }

        public dynamic GetDetail(string id)
        {
            var query = RequestWrapper
               .InstanceFromRequest()
               .SetRequestData("Byjlmx_BillNo", id)
               .LoadSettingXmlString(@"
            <settings defaultOrderBy='Byjlmx_Flbh'>
                <select>
                    A.*
                </select>
                <from>
                    b_Byjlmx A
                </from>
                <where>
                    <field name='Byjlmx_BillNo' cp='equal'></field>
                </where>
            </settings>");

            var pQuery = query.ToParamQuery();
            var result = new b_ByjlmxService().GetDynamicListWithPaging(pQuery);
            return result;

        }


        public List<dynamic> GetBillNo(string q)
        {
            var pQuery = ParamQuery.Instance().Select("top 10 Byjl_BillNo").AndWhere("Byjl_BillNo", q, Cp.StartWith);
            return new b_ByjlService().GetDynamicList(pQuery);
        }

        public List<dynamic> GetDoNames(string q)
        {
            var pQuery = ParamQuery.Instance().Select("distinct top 10 Byjl_Byr").AndWhere("Byjl_Byr", q, Cp.StartWithPY);
            return new b_ByjlService().GetDynamicList(pQuery);
        }

        public List<dynamic> GetApNames(string q)
        {
            var pQuery = ParamQuery.Instance().Select("distinct top 10 ApprovePerson").AndWhere("ApprovePerson", q, Cp.StartWithPY);
            return new b_ByjlService().GetDynamicList(pQuery);
        }


        public void Edit(dynamic data)
        {
            //   <columns ignore = 'Xhjh_BillNo'></columns>
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Byjl_BillNo' cp='equal'></field></where>
            </settings>", typeof(b_Byjl).Name);

            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where>
                    <field name='Byjlmx_BillNo' cp='equal'></field>
                    <field name='Byjlmx_Flbh'  cp='equal'></field>
                </where>
            </settings>", typeof(b_Byjlmx).Name);

            var result = new b_ByjlService().Edit(formWrapper, listWrapper, data);
        }

        public void Delete(string id)
        {
            var result = new b_ByjlService().Delete(ParamDelete.Instance().AndWhere("Byjl_BillNo", id));
            if (result <= 0)
            {
                DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Byjl_BillNo={0}]，请重试或联系管理员！", id);
            }
            else
            {
                new b_ByjhmxService().Delete(ParamDelete.Instance().AndWhere("Byjlmx_BillNo", id));
            }
        }


        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new DevService().Audit(typeof(b_Byjl).Name, "Byjl_BillNo", id, status, comment);
            DevHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Byjl_BillNo={0}]，请重试或联系管理员！", id);
        }

    }
}
